var express = require('express');
const Result = require('../lib/Result');
const Goods = require('../db/models/goods/goods');
const isRule = require('../util/isRule');
const Category = require('../db/models/category/category');
const GoodsBanner = require('../db/models/goods/goods_banner');
const GoodsType = require('../db/models/goods/goods_type');
const GoodsTypeValue = require('../db/models/goods/goods_type_value');
const GoodsSkus = require('../db/models/goods/goods_skus');
var router = express.Router();
/**
 * 创建商品,token需要135号权限，或13号权限，或6号权限,或super==1
 */
router.post("", (req, res) => {
    let token = req.auth
    if (isRule(token.rule, [135, 13, 6], res, token.super)) {
        Goods.create(req.body).then(value => {
            res.json(Result.success(value.toJSON()))
        }).catch(err => {
            res.json(Result.validateFailed())
        })
    }
})

/**
 * 批量上架下架商品,token需要134号权限，或13号权限，或6号权限,或super==1
 */
router.post("/changestatus", (req, res) => {
    let token = req.auth
    if (isRule(token.rule, [134, 13, 6], res, token.super)) {
        Goods.update({ status: req.body.status }, {
            where: {
                id: req.body.ids
            }
        }).then(row => {
            if (row == 0) {
                return
            }
            res.json(Result.success(row[0]))
        }).catch(err => {
            console.log(row);
            res.json(Result.unknownError())
        }
        )
    }
})
/**
 * 批量删除商品,token需要133号权限，或13号权限，或6号权限,或super==1
 */
router.post("/delete_all", (req, res) => {
    let token = req.auth
    if (isRule(token.rule, [133, 13, 6], res, token.super)) {
        Goods.deleteAll(req.body.ids).then(length => {
            res.json(Result.success(length))
        })
    }
})
/**
 * 批量恢复商品,token需要131号权限，或13号权限，或6号权限,或super==1
 */
router.post("/restore", (req, res) => {
    let token = req.auth
    if (isRule(token.rule, [131, 13, 6], res, token.super)) {
        Goods.restores(req.body.ids).then(length => {
            res.json(Result.success(length))
        })
    }
})
/**
 * 彻底删除商品,token需要132号权限，或13号权限，或6号权限,或super==1
 */
router.post("/destroy", (req, res) => {
    let token = req.auth
    let ids = req.body.ids
    if (isRule(token.rule, [132, 13, 6], res, token.super)) {
        Goods.destroy({
            where: {
                id: ids
            },
            force: true
        }).then(length => {
            res.json(Result.success(length))
        })
    }
})
/**
 * 更新商品规格，142，13，6
 */
router.post("/updateskus/:id", (req, res) => {
    let token = req.auth
    let sku_type = req.body.sku_type
    let sku_value = req.body.sku_value
    let goodsSkus = req.body.goodsSkus
    if (isRule(token.rule, [142, 13, 6], res, token.super)) {
        if (sku_type == 0) {
            Goods.update({
                sku_type: sku_type,
                sku_value: sku_value
            }, {
                where: {
                    id: req.params.id
                }
            }).then(res.json(Result.success(true)))
        } else {
            GoodsSkus.updateGoodsSkus(req.params.id,goodsSkus).then(res.json(Result.success(true)))
        }
    }
})
/**
 * 设置商品轮播图,token需要129号权限，或13号权限，或6号权限,或super==1
 */
router.post("/banners/:id", (req, res) => {
    let token = req.auth;

    if (isRule(token.rule, [129, 13, 6], res, token.super)) {
        GoodsBanner.addBanner(req.params.id, req.body.banners).then(value => {
            res.json(Result.success(value))
        })
    }
})
/**
 * 审核商品,token需要137号权限，或13号权限，或6号权限,或super==1
 */
router.post("/:id/check", (req, res) => {
    let token = req.auth
    let ischeck = req.body.ischeck
    let id = req.params.id
    if (ischeck) {
        return res.json(Result.validateFailed())
    }
    if (isRule(token.rule, [137, 13, 6], res, token.super)) {
        Goods.update({ ischeck: ischeck }, {
            where: {
                id: id
            }
        })
    }
})
/**
 * 更新商品,token需要136号权限，或13号权限，或6号权限,或super==1
 */
router.post("/:id", (req, res) => {
    let token = req.auth
    if (isRule(token.rule, [136, 13, 6], res, token.super)) {
        Goods.update(
            req.body,
            {
                where: {
                    id: req.params.id
                }
            }).then(
                res.json(Result.success(true))
            ).catch(
                res.json(Result.validateFailed())
            )
    }
})


/**
 * 查看商品资料,token需要127,13,6
 */
router.get("/read/:id", (res, req) => {
    let token = res.auth
    if (isRule(token.rule, [127, 13, 6], res, token.super)) {
        Goods.findOne({
            include: [
                {
                    model: GoodsType,
                    include: [
                        {
                            model: GoodsTypeValue
                        }
                    ]
                }, {
                    model: GoodsBanner
                }
            ],
            where: {
                id: res.params.id
            }
        }).then(value => {
            req.json(Result.success(value.toJSON()))
        })
    }
})
/**
 * 获取商品列表,token需要61,13,6
 */
router.get("/:page", (res, req) => {
    let token = res.auth
    let limit = res.query.limit ? res.query.limit : 10;
    let tab = res.query.tab//类型：all全部，checking审核中，saling出售中，off已下架，min_stock库存预警，delete回收站
    let category_id = res.query.category_id
    let title = res.query.title//模糊匹配
    if (!tab) {
        return req.json(Result.validateFailed())
    }
    if (isRule(token.rule, [61, 13, 6], res, token.super)) {
        Goods.getJson(res.params.page, limit, tab, category_id, title).then(json => [
            req.json(Result.success(json))
        ])
    }
})

module.exports = router